package ConveyorBelt.Actors;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.state.BasicGameState;

import ConveyorBelt.Base.Body;
import ConveyorBelt.Base.Entity;
import ConveyorBelt.Base.Level;
import ConveyorBelt.Map.Map;

public class Switch extends Body {
	public Map parent;
	public Image image;
	public float rotate;
	public int currentDirection;
	public ArrayList<Integer> blockedDirection;
	public enum direction {
		down, left, up, right
	}
	public boolean[][] blocked;
	boolean mouseCursorInSwitch = false;
	
	public Switch(Map map, Role role, Shape shape, Image image, String direction) {
		super(role, shape);
		this.parent = map;
		this.image = image;
		rotate = this.image.getRotation();
		currentDirection = getCurrentDirection(direction);
		blockedDirection = getBlockedDirection();
	}
	@Override
	public void render(BasicGameState bgs, Graphics g) {
		image.draw(getX(), getY());
		if(mouseCursorInSwitch) g.drawRect(getX(), getY(), image.getWidth(), image.getHeight());
	}

	@Override
	public void update(GameContainer gc, int delta) {
		Input input = gc.getInput();
		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();
		
		if(mouseX >= getX() && mouseX <= (getX()+image.getWidth()) &&  mouseY >= getY() && mouseY <= (getY() + image.getHeight())) {
			mouseCursorInSwitch = true;
		}
		else mouseCursorInSwitch = false;
		
		if(mouseCursorInSwitch) {
			if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
				System.out.print("Direction: "+currentDirection + "to ");
				do {
					rotate += 90;
					++currentDirection;
					if(currentDirection > 3) currentDirection = 0;
				} while(blockedDirection.contains(currentDirection));
				image.setRotation(rotate);
				if(rotate == 360) rotate = 0;
				System.out.println("Direction: "+currentDirection);
			}
		}
	}
	
	private int getCurrentDirection(String direct) {
		return direction.valueOf(direct).ordinal();
	}

	public ArrayList<Integer> getBlockedDirection() {
		ArrayList<Integer> tmp = new ArrayList<Integer>();
		
		blocked = parent.getCollisionMatrix("blocked");
		int tx = (int) (getX()-parent.mapX)/32;
		int ty = (int) (getY()-parent.mapY)/32;
		if(blocked[tx+1][ty]) tmp.add(direction.right.ordinal());
		if(blocked[tx-1][ty]) tmp.add(direction.left.ordinal());
		if(blocked[tx][ty+1]) tmp.add(direction.down.ordinal());
		if(blocked[tx][ty-1]) tmp.add(direction.up.ordinal());
		return tmp;
	}
	
	public void addBlockedDirection(int direction) {
		blockedDirection.add(direction);
	}
	@Override
	public Role getRole() {
		return Role.SWITCH;
	}

	@Override
	public void addToLevel(Level l) {
		l.add(this);
	}

	@Override
	public void removeFromLevel(Level l) {
		l.remove(this);
	}

	@Override
	public void onCollision(Entity obstacle) {
		//Nothing Happen when other Entities collide with Switch
	}

	@Override
	public boolean isToRemove() {
		return false;
	}
}
